package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.SpringContextBaseHolder;
import com.wxy.ej.flow.modules.taskflow.bean.TaskFlowLog;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.service.beanService.TaskFlowLogService;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
public class ControllerEndNodeStrategy extends NodeStrategy {
    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto) {
        List<String> logList = dataDto.getLogList();
        Long taskFlowId = dataDto.getTaskFlowId();

        TaskFlowLog log = new TaskFlowLog();
        log.setTtfTaskFlowId(taskFlowId);
        log.setLog(JSONArray.toJSONString(logList));
        // user info
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String userDetailsJson = request.getParameter("userDetailsJson");
            log.setCreatorUpdatorInfo(userDetailsJson);
        }catch (Exception e){}

        TaskFlowLogService taskFlowLogService = SpringContextBaseHolder.getBean("taskFlowLogService");
        taskFlowLogService.save(log);

        return Result.builder();
    }
}
